package com.cet.test.cykj;

import java.util.Arrays;

/**
 * @program: cet-practice
 * @description:
 * @author: 陈恩涛
 * @create: 2023-05-20 14:20
 **/
public class Main {

    /**
     * 输入有序数组，输出平方后的有序数组
     * @param nums 有序数组
     * @return 结果
     */
    public int[] poweredSortedArray(int[] nums) {

        int left = 0;
        int right = nums.length - 1;

        int[] result = new int[nums.length];

        // 两端的元素的平方后结果一定会产生最大值
        int i = nums.length - 1;

        while (left <= right) {
            if (nums[left] * nums[left] >= nums[right] * nums[right]) {
                result[i] = nums[left] * nums[left];
                left++;
            } else {
                result[i] = nums[right] * nums[right];
                right--;
            }
            i--;
        }

        return result;
    }
}

class Client{

    public static void main(String[] args) {
        Main main = new Main();

        int[] nums = new int[]{-4, -1, 0, 2, 3};
        System.out.println(Arrays.toString(main.poweredSortedArray(nums)));
    }
}

